﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
//using System.Windows.Forms;

public partial class OnlyAuthenticated_OnlyManagers_EditSpecialitati : System.Web.UI.Page
{
    String selectedItems;
    SqlConnection connection = new SqlConnection("Server=27424b2d-be0f-46c5-b105-a1ab015ef95f.sqlserver.sequelizer.com;Database=db27424b2dbe0f46c5b105a1ab015ef95f;User ID=flvirntmiujloika;Password=wVBm2uXivJGURbWJuu7sJZJEpKRsuhbTDCJbPovZKR8g7aTsbUgnwhyEm65qKDWn;");
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        //selectedItems = ListBox1.SelectedValue;
        //TextBox2.Text = selectedItems;

 
    }


    protected void Button2_Click(object sender, EventArgs e)
    {
        string remove;
        int nrItems = ListBox1.Items.Count;
        int i2 = 0;
        //if (ListBox1.SelectedItem.Text != null)
        
            for (int i = 0; i < nrItems; i++)
            {
                if (ListBox1.Items[i].Selected)
                {
                    remove = ListBox1.Items[i].Value;
                    DeleteRecord(remove);
                }
            }

           // for (int i = 0; i < nrItems; i++)
        //nrItems = ListBox1.Items.Count -1;   
        //while(i2 <= nrItems)
        //   {

        //        if (ListBox1.Items[i2].Selected)
        //        {
        //            remove = ListBox1.Items[i2].Value;
        //            ListBox1.Items.Remove(remove);
        //            nrItems--;
        //        }
        //        else i2++;
                
        //    }
        //   i2 = 0;

           ListBox1.DataBind();
    }


    private void DeleteRecord(string NumeSpecialitate)
    {
        
        string sqlStatement = "DELETE FROM DenumiriSpecialitati WHERE NumeSpecialitate = @NumeSpecialitate";

        try
        {
            connection.Open();
            SqlCommand cmd = new SqlCommand(sqlStatement, connection);
            cmd.Parameters.AddWithValue("@NumeSpecialitate", NumeSpecialitate);
            //cmd.CommandType = CommandType.Text;
            cmd.ExecuteNonQuery();
        }
        catch (System.Data.SqlClient.SqlException ex)
        {
            string msg = "Deletion Error:";
            msg += ex.Message;
            throw new Exception(msg);
        }
        finally
        {
            connection.Close();
        }
    }





       

    protected void Button1_Click(object sender, EventArgs e)
    {
        InsertRecord(TextBox1.Text);
        TextBox1.Text = "";
        ListBox1.DataBind(); //force refresh

        //Page.ClientScript.RegisterStartupScript(this.GetType(), "ErrorAlert", "alert('Specializarea a fost introdusa');", true);

        //Response.Write("<script>alert('Hello');</script>");
        //if (serverName.Text.Length == 0)
        //{

        //    // Initializes the variables to pass to the MessageBox.Show method.

        //    string message = "You did not enter a server name. Cancel this operation?";
        //    string caption = "Error Detected in Input";
        //    MessageBoxButtons buttons = MessageBoxButtons.YesNo;
        //    DialogResult result;

        //    // Displays the MessageBox.

        //    result = MessageBox.Show(message, caption, buttons);

        //    if (result == System.Windows.Forms.DialogResult.Yes)
        //    {

        //        // Closes the parent form.

        //        this.Close();

        //    }

        //}



    }

    private void InsertRecord(String NumeSpecialitate) { 
    
   //     string sqlStatement = "INSERT INTO DenumiriSpecialitati (NumeSpecialitate) VALUES (@NumeSpecialitate)";
  //      string sqlStatementWithCheck = "IF NOT EXISTS (SELECT ID, NumeModel FROM DenumiriModele WHERE NumeModel = @numeModel) BEGIN INSERT INTO DenumiriModele (NumeModel, TextModel, Specialitate, UserEditor) VALUES (@numeModel, @textModel, @numeSpecialitate, @currentUser1) END";
        string sqlStatement = "IF NOT EXISTS (SELECT NumeSpecialitate FROM DenumiriSpecialitati WHERE NumeSpecialitate = @NumeSpecialitate) BEGIN INSERT INTO DenumiriSpecialitati (NumeSpecialitate) VALUES (@NumeSpecialitate) END";
        try
        {
            connection.Open();
            SqlCommand cmd = new SqlCommand(sqlStatement, connection);
            cmd.Parameters.AddWithValue("@NumeSpecialitate", NumeSpecialitate);
            //cmd.CommandType = CommandType.Text;
            cmd.ExecuteNonQuery();
        }
        catch (System.Data.SqlClient.SqlException ex)
        {
            string msg = "Deletion Error:";
            msg += ex.Message;
            throw new Exception(msg);
        }
        finally
        {
            connection.Close();
        }    
    
    }

    protected void Button3_Click(object sender, EventArgs e)
    {
        ListBox1.DataBind();
    }
}